package org.spring.blog.controller.admin;

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.spring.blog.pojo.Category;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.ICategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@PreAuthorize("@permission.admin()")
@RestController
@RequestMapping("/admin/category")
public class CategoryAdminApi {
    final
    ICategoryService categoryService;

    public CategoryAdminApi(ICategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 添加分类
     * 要求
     * 1.管理员权限
     *
     * @param _category 分类对象
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.category_name_cant_empty_code, message = ResponseState.category_name_cant_empty_message),
            @ApiResponse(code = ResponseState.category_description_cant_empty_code, message = ResponseState.category_description_cant_empty_message),
            @ApiResponse(code = ResponseState.category_pinyin_cant_empty_code, message = ResponseState.category_pinyin_cant_empty_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PostMapping
    public ResponseResult insertCategory(@RequestBody Category _category) {
        return categoryService.insertCategory(_category);
    }

    /**
     * 删除分类
     *
     * @param _categoryId 分类对象id
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @DeleteMapping("/{categoryId}")
    public ResponseResult deleteCategory(@PathVariable("categoryId") String _categoryId) {
        return categoryService.deleteCategory(_categoryId);
    }

    /**
     * 更新分类
     *
     * @param categoryId 分类id
     * @param _category  分类对象
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PutMapping("/{categoryId}")
    public ResponseResult updateCategory(@PathVariable("categoryId") String categoryId, @RequestBody Category _category) {
        return categoryService.updateCategory(categoryId, _category);
    }

    /**
     * 获取分类
     * <p>
     * 需要管理员权限
     *
     * @param _categoryId 分类id
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.data_not_have_code, message = ResponseState.data_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/{categoryId}")
    public ResponseResult queryCategory(@PathVariable("categoryId") String _categoryId) {
        return categoryService.queryCategory(_categoryId);
    }

    /**
     * 获取分类列表
     *
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/list")
    public ResponseResult listCategories() {
        return categoryService.listCategoriesForAdmin();
    }
}
